﻿using Common.Device.DeviceDb;
using Common.Device.Enums;
using Common.Device.Interfaces;
using HandyControl.Tools.Extension;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Device.Views
{
    /// <summary>
    /// DeviceSetView.xaml 的交互逻辑
    /// </summary>
    public partial class DeviceSetView : UserControl
    {
        private ViewModels.DeviceSetViewModel viewModel = new ViewModels.DeviceSetViewModel();
        public DeviceSetView()
        {
            InitializeComponent();
            DataContext = viewModel;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //添加/更改使用仪器
            try
            {
                //仪器型号，仪器标识，仪器编号为空
                if (string.IsNullOrEmpty(viewModel.Model) || string.IsNullOrEmpty(viewModel.Name) || string.IsNullOrEmpty(viewModel.Mode) || string.IsNullOrEmpty(viewModel.Port) || string.IsNullOrEmpty(viewModel.Parameter))
                {
                    HandyControl.Controls.MessageBox.Warning("请先完善仪器信息！");
                    return;
                }
                UseDevice device = null;
                //使用仪器被选中
                if (DeviceList.SelectedItem != null)
                {
                    //提示 是，否，取消
                    var result = View.Controls.Dialogs.TipView.Show("发现有设备被选中,是否对所选设备进行更改？", new Dictionary<string, Brush> { { "更改设备", View.Base.Colors.PrimaryColor }, { "新增设备", View.Base.Colors.SuccessColor }, { "取消操作", View.Base.Colors.DangerColor } });
                    //取消
                    if (result == "取消操作")
                    {
                        return;
                    }
                    //是，进行更改
                    if (result == "更改设备")
                    {
                        device = DeviceList.SelectedItem as UseDevice;
                        //提示标识发生变化
                        if (device.name != viewModel.Name)
                        {
                            result = View.Controls.Dialogs.TipView.Show("发现设备标识变化,是否对所选设备进行更改？", new Dictionary<string, Brush> { { "更改设备", View.Base.Colors.PrimaryColor }, { "取消操作", View.Base.Colors.DangerColor } });
                            //取消
                            if (result == "取消操作")
                            {
                                return;
                            }
                            //更改设备
                            if (result == "更改设备")
                            {
                                DeviceService.iDevices.Remove(device.name);
                                DeviceDbHelper.db.Deleteable(device).ExecuteCommand();
                                device.name = viewModel.Name;
                            }
                        }
                        //移除更改前的仪器
                        DeviceService.iDevices.Remove(device.name);
                        //赋值更改后的参数
                        device.name = viewModel.Name;
                        device.temp = (DeviceEnum.DeviceTempEnum)Enum.Parse(typeof(DeviceEnum.DeviceTempEnum), viewModel.Temp);
                        device.model = (DeviceEnum.DeviceModelEnum)Enum.Parse(typeof(DeviceEnum.DeviceModelEnum), viewModel.Model);
                        device.type = (DeviceEnum.DeviceTypeEnum)Enum.Parse(typeof(DeviceEnum.DeviceTypeEnum), viewModel.Type);
                        device.mode = (DeviceEnum.DeviceModeEnum)Enum.Parse(typeof(DeviceEnum.DeviceModeEnum), viewModel.Mode);
                        device.port = viewModel.Port;
                        device.parameter = viewModel.Parameter;
                        device.parameter2 = viewModel.Parameter2;
                        device.descripe = viewModel.Descripe;
                        device.addr = viewModel.Addr;
                    }
                }
                else
                {
                    if (DeviceDbHelper.db.Queryable<UseDevice>().Count(o => o.name == viewModel.Name) > 0)
                    {
                        HandyControl.Controls.MessageBox.Warning("设备标识重复");
                        return;
                    }
                    //新增使用仪器
                    device = device ?? new UseDevice();
                    //赋值参数
                    device.temp = (DeviceEnum.DeviceTempEnum)Enum.Parse(typeof(DeviceEnum.DeviceTempEnum), viewModel.Temp);
                    device.name = viewModel.Name;
                    device.type = (DeviceEnum.DeviceTypeEnum)Enum.Parse(typeof(DeviceEnum.DeviceTypeEnum), viewModel.Type);
                    device.addr = viewModel.Addr;
                    device.model = (DeviceEnum.DeviceModelEnum)Enum.Parse(typeof(DeviceEnum.DeviceModelEnum), viewModel.Model);
                    device.mode = (DeviceEnum.DeviceModeEnum)Enum.Parse(typeof(DeviceEnum.DeviceModeEnum), viewModel.Mode);
                    device.port = viewModel.Port;
                    device.parameter = viewModel.Parameter;
                    device.parameter2 = viewModel.Parameter2;
                    device.descripe = viewModel.Descripe;
                }
                if (device==null)
                {
                    return;
                }
                //上传至数据库
                var isOK = DeviceDbHelper.UpUseDevice(device);
                //上传失败
                if (!isOK)
                {
                    HandyControl.Controls.MessageBox.Warning("添加/更改设备失败");
                    return;
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error("添加/更改设备失败" + ex.Message);
            }
            finally
            {
                viewModel.UseDevices = new ObservableCollection<UseDevice>(DeviceService.useDevices.Values);
            }
        }

        private void DeviceList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //双击解析仪器参数到编辑区
            var device = DeviceList.SelectedItem as UseDevice;
            if (device != null)
            {
                viewModel.Temp = device.temp.ToString();
                viewModel.Type = device.type.ToString();
                viewModel.Model = device.model.ToString();
                viewModel.Mode = device.mode.ToString();
                viewModel.Port = device.port;
                viewModel.Parameter = device.parameter;
                viewModel.Addr = device.addr;
                viewModel.Parameter2 = device.parameter2;
                viewModel.Name = device.name;
                viewModel.Descripe = device.descripe;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            viewModel.UseDevices = new ObservableCollection<UseDevice>(DeviceService.useDevices.Values);
        }
    }
}
